import java.awt.Point;
import java.awt.Rectangle;

import com.speljohan.rsbot.script.Random;
import com.speljohan.rsbot.script.wrappers.RSInterface;
import com.speljohan.rsbot.script.wrappers.RSInterfaceChild;
import com.speljohan.rsbot.script.wrappers.RSNPC;

/**
 * @author Pwnaz0r & Velocity
 * @version 2.3 - 04/03/09
 */
public class BeehiveSolver extends Random {
	RSNPC BeehiveKeeper;
	int BeehiveKeeperID = 8649;
	int[] destInterfaces = { 16, 17, 18, 19 };
	int ID_Down = 16034;
	int ID_MidDown = 16022;
	int ID_MidUp = 16025;
	int ID_Top = 16036;
	int[] BeehiveArrays = { ID_Top, ID_MidUp, ID_MidDown, ID_Down };
	RSInterfaceChild INTERFACE_BUILDBEEHIVE = getInterface(420, 40);
	String[] ModelNames = { "Top", "Middle Up", "Middle Down", "Down" };
	boolean solved;
	int[] starInterfaces = { 12, 13, 14, 15 };

	@Override
	public boolean activateCondition() {
		if (!isLoggedIn()) {
			return false;
		}

		BeehiveKeeper = getNearestNPCByID(BeehiveKeeperID);
		if (BeehiveKeeper != null || getBeehiveInterface().isValid()) {
			wait(random(1000, 1500));
			BeehiveKeeper = getNearestNPCByID(BeehiveKeeperID);
			if (BeehiveKeeper != null || getBeehiveInterface().isValid()) {
				solved = false;
				wait(random(1000, 1500));
				return true;
			}
		}

		return false;
	}

	public boolean dragInterfaces(final RSInterfaceChild child1,
			final RSInterfaceChild child2) {
		final Point start = returnMidInterface(child1);
		final Point finish = returnMidInterface(child2);

		moveMouse(start);
		dragMouse(finish);
		return true;
	}

	@Override
	public String getAuthor() {
		return "Pwnaz0r & Velocity";
	}

	public RSInterface getBeehiveInterface() {
		return getInterface(420); // 420 <3
	}

	@Override
	public String getName() {
		return "Bee Hive Random";
	}

	@Override
	public double getVersion() {
		return 2.3;
	}

	@Override
	public int loop() {
		if (solved) {
			wait(random(1000, 1500));
			solved = false;
			return -1;
		}
		BeehiveKeeper = getNearestNPCByID(BeehiveKeeperID);
		if (BeehiveKeeper == null) {
			log.severe("Could not find beekeeper.");
			return -1;
		}

		if (myClickContinue()) {
			return 200;
		}

		if (atInterface(236, 2)) {
			return random(800, 1200);
		}

		if (getBeehiveInterface().isValid()) {
			for (int i = 1; i < 5; i++) {
				log.info("Checking ID: " + i);
				final int id = returnIdAtSlot(i);
				dragInterfaces(getBeehiveInterface().getChild(
						starInterfaces[i - 1]), getBeehiveInterface().getChild(
						returnDragTo(id)));
			}
			solved = true;
			if (atInterface(INTERFACE_BUILDBEEHIVE)) {
				return random(900, 1600);
			}
			log.info("Returning -1");
			return -1;
		} else {
			log.info("Interfaces not valid.");
		}

		if (getMyPlayer().getInteracting() == null) {
			final RSNPC npc = getNearestNPCByID(BeehiveKeeperID);
			if (npc != null) {
				if (!clickCharacter(npc, "Talk-to")) {
					setCameraRotation(getCameraAngle() + random(-30, 30));
				}
			}
		}

		return random(500, 1000);
	}

	public boolean myClickContinue() {
		wait(random(800, 1000));
		return atInterface(243, 7) || atInterface(241, 5)
				|| atInterface(242, 6) || atInterface(244, 8)
				|| atInterface(64, 5);
	}

	public int returnDragTo(final int Model) {
		switch (Model) {
		case 16036:
			return destInterfaces[0];
		case 16025:
			return destInterfaces[1];
		case 16022:
			return destInterfaces[2];
		case 16034:
			return destInterfaces[3];
		default:
			return -1;
		}
	}

	public int returnIdAtSlot(final int slot) {
		if (slot < 1 || slot > 4) {
			log.info("Invalid Slot.");
			stopAllScripts();
		}

		int Model_ID = -1;
		Model_ID = getBeehiveInterface().getChild(returnSlotId(slot))
				.getModelID();

		if (Model_ID == -1) {
			log.info("Could not retrieve ID.");
			stopAllScripts();
		}

		for (int i = 0; i < BeehiveArrays.length; i++) {
			if (Model_ID == BeehiveArrays[i]) {
				log
						.info("Slot " + slot + " contains section: "
								+ ModelNames[i]);
				return Model_ID;
			}
		}

		return -1;
	}

	public Point returnMidInterface(final RSInterfaceChild child) {
		Point point = new Point(-1, -1);
		final Rectangle rect = child.getArea();
		if (rect != null) {
			point = new Point((int) rect.getCenterX(), (int) rect.getCenterY());
		}
		return point;
	}

	public int returnSlotId(final int slot) {
		switch (slot) {
		case 1:
			return 25;
		case 2:
			return 22;
		case 3:
			return 23;
		case 4:
			return 21;
		default:
			log.info("Invalid slot ID.");
			stopAllScripts();
			break;
		}
		return -1;
	}
}